home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / FLTK-1.0.6 / src / Fl_Dial.cxx < prev    next >
Encoding:
C/C++ Source or Header  |  1999-03-10  |  3.9 KB  |  136 lines

  1. //
  2. // "$Id: Fl_Dial.cxx,v 1.12 1999/03/10 08:17:42 bill Exp $"
  3. //
  4. // Circular dial widget for the Fast Light Tool Kit (FLTK).
  5. //
  6. // Copyright 1998-1999 by Bill Spitzak and others.
  7. //
  8. // This library is free software; you can redistribute it and/or
  9. // modify it under the terms of the GNU Library General Public
  10. // License as published by the Free Software Foundation; either
  11. // version 2 of the License, or (at your option) any later version.
  12. //
  13. // This library is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16. // Library General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Library General Public
  19. // License along with this library; if not, write to the Free Software
  20. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  21. // USA.
  22. //
  23. // Please report all bugs and problems to "fltk-bugs@easysw.com".
  24. //
  25.  
  26. #include <FL/Fl.H>
  27. #include <FL/Fl_Dial.H>
  28. #include <FL/fl_draw.H>
  29. #include <stdlib.h>
  30. #include <FL/math.h>
  31.  
  32. // All angles are measured with 0 to the right and counter-clockwise
  33.  
  34. void Fl_Dial::draw(int x, int y, int w, int h) {
  35.   if (damage()&FL_DAMAGE_ALL) draw_box(box(), x, y, w, h, color());
  36.   x += Fl::box_dx(box());
  37.   y += Fl::box_dy(box());
  38.   w -= Fl::box_dw(box());
  39.   h -= Fl::box_dh(box());
  40.   double angle = (a2-a1)*(value()-minimum())/(maximum()-minimum()) + a1;
  41.   if (type() == FL_FILL_DIAL) {
  42.     // foo: draw this nicely in certain round box types
  43.     int foo = (box() > _FL_ROUND_UP_BOX && Fl::box_dx(box()));
  44.     if (foo) {x--; y--; w+=2; h+=2;}
  45.     fl_color(color());
  46.     fl_pie(x, y, w-1, h-1, 270-a1, angle > a1 ? 360+270-angle : 270-360-angle);
  47.     fl_color(selection_color());
  48.     fl_pie(x, y, w-1, h-1, 270-angle, 270-a1);
  49.     if (foo) {
  50.       fl_color(FL_BLACK);
  51.       fl_arc(x, y, w, h, 0, 360);
  52.     }
  53.     return;
  54.   }
  55.   if (!(damage()&FL_DAMAGE_ALL)) {
  56.     fl_color(color());
  57.     fl_pie(x+1, y+1, w-2, h-2, 0, 360);
  58.   }
  59.   fl_push_matrix();
  60.   fl_translate(x+w/2-.5, y+h/2-.5);
  61.   fl_scale(w-1, h-1);
  62.   fl_rotate(45-angle);
  63.   fl_color(selection_color());
  64.   if (type()) { // FL_LINE_DIAL
  65.     fl_begin_polygon();
  66.     fl_vertex(0.0,   0.0);
  67.     fl_vertex(-0.04, 0.0);
  68.     fl_vertex(-0.25, 0.25);
  69.     fl_vertex(0.0,   0.04);
  70.     fl_end_polygon();
  71.     fl_color(FL_BLACK);
  72.     fl_begin_loop();
  73.     fl_vertex(0.0,   0.0);
  74.     fl_vertex(-0.04, 0.0);
  75.     fl_vertex(-0.25, 0.25);
  76.     fl_vertex(0.0,   0.04);
  77.     fl_end_loop();
  78.   } else {
  79.     fl_begin_polygon(); fl_circle(-0.20, 0.20, 0.07); fl_end_polygon();
  80.     fl_color(FL_BLACK);
  81.     fl_begin_loop(); fl_circle(-0.20, 0.20, 0.07); fl_end_loop();
  82.   }
  83.   fl_pop_matrix();
  84. }
  85.  
  86. void Fl_Dial::draw() {
  87.   draw(x(), y(), w(), h());
  88.   draw_label();
  89. }
  90.  
  91. int Fl_Dial::handle(int event, int x, int y, int w, int h) {
  92.   switch (event) {
  93.   case FL_PUSH:
  94.     handle_push();
  95.   case FL_DRAG: {
  96.     int mx = Fl::event_x()-x-w/2;
  97.     int my = Fl::event_y()-y-h/2;
  98.     if (!mx && !my) return 1;
  99.     double angle = 270-atan2((float)-my, (float)mx)*180/M_PI;
  100.     double oldangle = (a2-a1)*(value()-minimum())/(maximum()-minimum()) + a1;
  101.     while (angle < oldangle-180) angle += 360;
  102.     while (angle > oldangle+180) angle -= 360;
  103.     double val;
  104.     if ((a1<a2) ? (angle <= a1) : (angle >= a1)) {
  105.       val = minimum();
  106.     } else if ((a1<a2) ? (angle >= a2) : (angle <= a2)) {
  107.       val = maximum();
  108.     } else {
  109.       val = minimum() + (maximum()-minimum())*(angle-a1)/(a2-a1);
  110.     }
  111.     handle_drag(clamp(round(val)));
  112.   } return 1;
  113.   case FL_RELEASE:
  114.     handle_release();
  115.     return 1;
  116.   default:
  117.     return 0;
  118.   }
  119. }
  120.  
  121. int Fl_Dial::handle(int e) {
  122.   return handle(e, x(), y(), w(), h());
  123. }
  124.  
  125. Fl_Dial::Fl_Dial(int x, int y, int w, int h, const char* l)
  126.   : Fl_Valuator(x, y, w, h, l) {
  127.   box(FL_OVAL_BOX);
  128.   selection_color(FL_INACTIVE_COLOR); // was 37
  129.   a1 = 45;
  130.   a2 = 315;
  131. }
  132.  
  133. //
  134. // End of "$Id: Fl_Dial.cxx,v 1.12 1999/03/10 08:17:42 bill Exp $".
  135. //
  136.